package vip.xiaonuo.hontu.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.core.util.DateUtils;
import vip.xiaonuo.modular.htpushdata.entity.HtPushData;
import vip.xiaonuo.modular.htpushdata.service.HtPushDataService;
import vip.xiaonuo.modular.securitydata.entity.SecurityData;
import vip.xiaonuo.modular.securitydata.service.SecurityDataService;
import vip.xiaonuo.modular.transdata.entity.TransData;
import vip.xiaonuo.modular.transdata.service.TransDataService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 定期清理过期数据
 * 1天执行一次
 * 原始数据保存1个月
 * 解析后的数据保存3个月
 */
@Component
@Slf4j
public class CleanHtExpiredDataTask implements TimerTaskRunner {

    @Resource
    private HtPushDataService htPushDataService;

    @Resource
    private TransDataService transDataService;

    @Resource
    private SecurityDataService securityDataService;

    @Override
    public void action(String jobParams, String taskId) {
        try {
            executeXxlJob();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void executeXxlJob() throws InterruptedException {
        Date date = DateUtils.addDay(new Date(),-30);
        htPushDataService.remove(new LambdaQueryWrapper<HtPushData>()
                .lt(HtPushData :: getCreateTime ,date));

        date = DateUtils.addDay(date,-60);
        transDataService.remove(new LambdaQueryWrapper<TransData>()
                .lt(TransData :: getCreateTime ,date));

        securityDataService.remove(new LambdaQueryWrapper<SecurityData>()
                .lt(SecurityData :: getCreateTime , date));
    }


}
